const axios = require("axios")
const fs = require("fs")
const cheerio = require("cheerio")

async function loadHW(keyword) {
    // https://openapi.vmall.com/rms/comment/getCommentList.json
    let options = {
        method: "POST" ,
        // scheme : "https" ,
        url: "https://openapi.vmall.com/rms/comment/getCommentList.json?t=1630972929202",
        headers: {
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Content-Length": "79",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "Cookie": "TID=53ace7e3baa7bb4090b8df8bc9ee8e42; deviceid=53ace7e3baa7bb4090b8df8bc9ee8e42; cps_id=10618; cps_track_id=1630941079529_e7f05l2szif; euid=1c1ea17844698969618882507addebace142687e5657a8db; CSRF-TOKEN=I3o6dKFnOFb4cQP5AhkX2nI2Mrq2bwR6XkKr; HWWAFSESID=95836715bdf1e74d040; HWWAFSESTIME=1630972591478; cartId=da4d74cb7a7f43a7960a5df0e7f9ebd7; showAds=true; sdevid=1654df8491c7d5475a5047ba6f7dbd743450226c; salePortal=1; recommendflag=0; ipaddress=%E5%8C%97%E4%BA%AC%2C%E5%8C%97%E4%BA%AC%2C%E4%B8%9C%E5%9F%8E%E5%8C%BA%2C6922; ipaddressId=3510%2C6921%2C6922; device_data=*2k75J1JUMT2zDSXTnWNZZ1ZMdEd5MAb2sm2UX0l2GTkG8JNZNRMUPW4W5mI5aMNNONR5ziiklii6uX13mx51iYJgcMYQdYNBZZMUOPNhbMBYSQ1T3mTDumDmlyWjlujTypTW0mWG065U0FZhIIVxUJcYJgAhV4my21T29FJM0EYNg9GDDWDXWTmCXzCTCWDCUAOMRMaYNEZPAEOZFBMJQQMTIJ9iDX09mTmjC3u4z0plXjzzjH1kqMYNMZYPZMTabMNNaPZYGWw92z1YcZZMPUEa5izw3mz4lkpoG39t3lNMakANOBQYNE0OZQEJMIlJMw8OlWT06GThsXjzwjkUjT22zXT51T2sIcUkIREB5dIhMMkRYED49jWjmJ1gMddNNd2Tm3TTHmHTjU2mCnmmYZJZOONNaMhAPZVcbdUQVQYMaMWml2XWy4mmywTTll2TwjWT3lDGfCNMOXYMZMYTNMJZYYMTiy3z3jHJ5MMldNNBzjhvh5mwulllw09sljPYQYYEZcaklNZUMOe4UMJBQMR4G0izDzwDUhslTwLkTuzTjywmmxkcF5JUUI9Z5AIBhVARIzhmy2hzWMJMcVJYYpWXDTWjSWGC2W2mi22PlMZYMhMMVhYMUZZY19YMNVaaIcmwjz31GSu9WjwwWjl0jnwzDTlxzVNYNZNOOPbJNJJZYMMlmD2yDS9LZcJE42mvCwX5mZMYIZUNNa4ZtLj5TuWxGsTlDjDUgNRlIV4M2y2jY3Rd",
            "CsrfToken": "I3o6dKFnOFb4cQP5AhkX2nI2Mrq2bwR6XkKr",
            "Host": "openapi.vmall.com",
            "Origin": "https://www.vmall.com",
            "Referer": "https://www.vmall.com/",
            "sec-ch-ua": '"Google Chrome";v="93", " Not;A Brand";v="99", "Chromium";v="93"',
            'sec-ch-ua-mobile': '?0',
            "sec-ch-ua-platform": '"Windows"',
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "same-site",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"
        },
        dataType:"json"
        // data : {
        //     // "PersonalizeSearch": '"2"',
        //     // "brandType": 0,
        //     // "country": '"CN"',
        //     // "keyword": 36,
        //     // "lang": '"zh-CN"',
        //     // "pageNum": 1,
        //     // "pageSize": 20,
        //     // "portal": 1,
        //     // "searchFlag": 1,
        //     // "searchId": '"53ace7e3baa7bb4090b8df8bc9ee8e42"',
        //     // "searchSortField": 0,
        //     // "searchSortType": '"desc"',
        //     // "tid": '"53ace7e3baa7bb4090b8df8bc9ee8e42"',
        // }
    }
    let response = await axios(options);
    console.log(response.data);

    // let $ = cheerio.load(response.data);

    // let top4 = $(".css-1dbjc4n .r-1udh08x .r-17gur6a .r-u8s1d .css-9pa8cd");
    // let img   = $(".p-img a img");
    // let title = $(".p-name.p-name-type-2 em");

    // console.log(top4.length);
    

    // let arr = [];
    // for(var i = 0 ; i < price.length ; i ++){
    //       arr.push( {
    //            price : price.eq(i).html(),
    //            src : img.eq(i).attr("data-lazy-img"),
    //            title : title.eq(i).text()
    //       });
    // }

    fs.writeFile("./static/data/detail.json", JSON.stringify(response) , err => {
        if (err) {
            throw err;
        }
        console.log("华为数据爬取成功");
    })
}

loadHW()